package com.sdyc.ndmp.cls.engine;

import com.google.common.collect.Sets;
import org.apache.commons.lang.StringUtils;
import scala.Tuple2;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;


/**
 *
 * 句子分词
 *
 * @author zhenqin
 */
public class DataKeywords {

    /**
     *
     * 文档的正文, 字符串正文
     *
     */
    protected final String text;

    /**
     * 文档被分词后的词距
     */
    protected Map<String, Tuple2<Integer, Integer>> keywordRange = new HashMap();


    /**
     * 句子和分词
     * @param text 句子，标题+正文
     * @param keywordRange 分词后的起始位置
     */
    public DataKeywords(String text, Map<String, Tuple2<Integer, Integer>> keywordRange) {
        this.text = StringUtils.trimToEmpty(text);

        if(keywordRange != null) {
            this.keywordRange = keywordRange;
        }
    }

    public String getText() {
        return text;
    }

    public Map<String, Tuple2<Integer, Integer>> getKeywordRange() {
        return keywordRange;
    }


    public Map<String, Set<Integer>> getKeywordRangeStart() {
        Map<String, Set<Integer>> r = new HashMap(keywordRange.size());
        for (Map.Entry<String, Tuple2<Integer, Integer>> entry : keywordRange.entrySet()) {
            r.put(entry.getKey(), Sets.newHashSet(entry.getValue()._1(), entry.getValue()._2()));
        }
        return r;
    }

    public Set<String> keywords() {
        return keywordRange.keySet();
    }
}
